package edu.faytechcc.csc.database;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

/**
 * This class reads a file and executes the line as an SQL statement.
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class PopulateFromTextFile
{

    public static void main(String[] args)
    {
        populate("AnimeFile.txt");
        populate("MangaFile.txt");
        
    }

    private static void populate(String filename)
    {
        // Create a string to hold the Database location
        final String DB_URL = "jdbc:derby:ARGDB;create=false";
        File file = new File(filename);
        try
        {
            // Create a scanner for the input file
            Scanner inputFile = new Scanner(file);
            // Create a connection to the database.
            Connection conn =
                    DriverManager.getConnection(DB_URL);
            while (inputFile.hasNext())
            {
                // Get Statement
                String statment = inputFile.nextLine();
                if (statment != null && !"﻿".equals(statment))
                {
                    System.out.println(statment);
                    // Create SQL Statment
                    Statement stmt = conn.createStatement();
                    // Insert row 
                    stmt.execute(statment);
                }
            }
            // Close the connection.
            conn.close();
            inputFile.close();
        } catch (Exception ex)
        {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }
}
